#include <string.h>
#include "gkt_malloc.h"
#include "gkt_crc.h"
#include "gkt_rfid.h"
#include "window_common.h"

#if defined(GKT_CONFIG_DOOR_VERIFY_LOCKED_ENABLE) && GKT_CONFIG_DOOR_VERIFY_LOCKED_ENABLE

#include "window_verify_locked.h"

#ifdef GKT_CONFIG_DOOR_VERIFY_LOCKED_RFID_KEY
static uint8_t s_rfid_card_key[] = { GKT_CONFIG_DOOR_VERIFY_LOCKED_RFID_KEY };
#else
static uint8_t s_rfid_card_key[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
#endif

#define CARD_SECTOR_MANUFACTURER	0
#define CARD_SECTOR_MAX	16
#define CARD_BLOCK_AUTH	3
#define CARD_BLOCK_MAX	4
#define CARD_BLOCK_SIZE	16

#define CARD_BLOCK_ADDR(sector_id, block_id)	\
	(((sector_id) * CARD_BLOCK_MAX) + (block_id))

static int window_verify_locked_read_block_data(
				gkt_rfid_info_s *rfid_info,
				uint32_t block_index, void *buffer)
{
	uint32_t sector_id, block_id, block_addr;
	int retval;

	sector_id = (block_index / CARD_BLOCK_AUTH) + 1;
	block_id = block_index % CARD_BLOCK_AUTH;
	block_addr = CARD_BLOCK_ADDR(sector_id, block_id);

	retval = gkt_rfid_auth_state(GKT_RFID_AUTH_A_KEY,
					(uint8_t)block_addr, rfid_info->id, 
					s_rfid_card_key);
	if (GKT_SUCCESS == retval)
		retval = gkt_rfid_read(block_addr, buffer);
	else {
		gkt_error("window_trial: auth (%u - %u) failed %d\n", 
						sector_id, block_id, retval);
	}

	return retval;
}

int window_verify_locked_update_status(gkt_rfid_info_s *rfid_info)
{
	window_verify_locked_s info;
	uint32_t block_index = 0;
	uint16_t crc16;
	int retval;

#if defined(GKT_CONFIG_RFID_QUERY_MODE_ENABLE) && GKT_CONFIG_RFID_QUERY_MODE_ENABLE	
	retval = gkt_rfid_get_info(rfid_info, 1);
	if (retval != GKT_SUCCESS) {
		retval = gkt_rfid_get_info(rfid_info, 1);
		if (retval != GKT_SUCCESS) {
			gkt_rfid_antenna_off();
			gkt_rfid_control(GKT_RFID_CTRL_ENABLE, 0);
			gkt_printf("verify_locked_update_image err.\n");
			return retval;
		}
	}
	gkt_rfid_control(GKT_RFID_CTRL_DISABLE, 1);
#endif
	retval = window_verify_locked_read_block_data(rfid_info,
					block_index, (void *)&info);
	if (retval != GKT_SUCCESS) {		
#if defined(GKT_CONFIG_RFID_QUERY_MODE_ENABLE) && GKT_CONFIG_RFID_QUERY_MODE_ENABLE	
		gkt_rfid_antenna_off();
		gkt_rfid_control(GKT_RFID_CTRL_ENABLE, 0);
#endif
		gkt_error("window_verify_locked: [UDT] read header block failed %d\n", retval);
		return retval;
	}

	gkt_info("window_verify_locked: [UDT] magic(0x%08x), status(%u), crc16(0x%04x)\n",
				info.magic, info.status, info.crc16);

	if (info.magic != WINDOW_VERIFY_LOCKED_MAGIC) {
#if defined(GKT_CONFIG_RFID_QUERY_MODE_ENABLE) && GKT_CONFIG_RFID_QUERY_MODE_ENABLE	
		gkt_rfid_antenna_off();
		gkt_rfid_control(GKT_RFID_CTRL_ENABLE, 0);
#endif
		gkt_info("window_verify_locked: [UDT] invalid header, unsupport!\n");
		return GKT_ENOTSUPPORT;
	}

#if defined(GKT_CONFIG_RFID_QUERY_MODE_ENABLE) && GKT_CONFIG_RFID_QUERY_MODE_ENABLE	
	gkt_rfid_antenna_off();
	gkt_rfid_control(GKT_RFID_CTRL_ENABLE, 0);
#endif

	/* check image data crc */
	crc16 = gkt_crc16_ccitt_false((uint8_t*)&info, 6);
	if (crc16 != info.crc16) {
		gkt_error("window_verify_locked: crc(0x%x) not correct.\n", crc16);
		return GKT_EUNMATCHED;

	}

	/* save image */
	gp_window_unreset_data->verify_locked_status = !gp_window_unreset_data->verify_locked_status;

	/* update */
	window_data_save();

	return GKT_SUCCESS;
}

#endif

